package com.example.cofeed.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.cofeed.R;
import com.example.cofeed.activity.AttentionMoreActivity;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.entity.Attention;
import com.lzy.okgo.model.Response;

public class AttentionRightAdapter extends BaseQuickAdapter<Attention, BaseViewHolder> {
    public AttentionRightAdapter() {
        super(R.layout.adapter_attention_right);
    }

    @Override
    protected void convert(BaseViewHolder helper, Attention item) {

        helper.setText(R.id.tv_title, item.title);
        if (item.ispush == 0) {
            helper.setImageResource(R.id.iv_type, R.mipmap.tianjiaguanzhu);
        } else {
            helper.setImageResource(R.id.iv_type, R.mipmap.quxiaoguanzhu);
        }

        RecyclerView recLv = helper.getView(R.id.rec_lv);

        if (ObjectUtils.isNotEmpty(item.third_menu)) {
            if (item.isExpend){
                helper.setImageResource(R.id.iv_type, R.mipmap.ic_up);
                recLv.setVisibility(View.VISIBLE);
            }else {
                helper.setImageResource(R.id.iv_type, R.mipmap.ic_down);
                recLv.setVisibility(View.GONE);
            }

            SubMoreAdapter cityAdapter = new SubMoreAdapter();
            recLv.setLayoutManager(new LinearLayoutManager(mContext));
            recLv.setAdapter(cityAdapter);
            cityAdapter.setNewData(item.third_menu);

            cityAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    final Attention city = (Attention) adapter.getData().get(position);
                    ((SubMoreAdapter) adapter).setSelect(city);

                    if (city.ispush == 1) {
//                        ApiService.followinfo_on_off("pushSubscriptionSet", 1, item.catid, city.chid, new DialogCallback<LzyResponse<Object>>() {
//                            @Override
//                            public void onSuccess(Response<LzyResponse<Object>> response) {
////                                city.ispush = 1;
////                                notifyDataSetChanged();
//                            }
//                        });
                        item.ispush = 0;

//                        ApiService.followinfo_add(this, item.ispush == 1? 0:1, item.catid, item.chid, new DialogCallback<LzyResponse<Object>>(mContext) {
                        ApiService.followinfo_add(this,city.catid, city.chid, city.areaid, new DialogCallback<LzyResponse<Object>>(mContext) {
                            @Override
                            public void onSuccess(Response<LzyResponse<Object>> response) {
                                cityAdapter.notifyDataSetChanged();

                            }
                        });
                    } else {
//                        ApiService.followinfo_on_off("pushSubscriptionSet", 0, item.catid, city.chid, new DialogCallback<LzyResponse<Object>>() {
//                            @Override
//                            public void onSuccess(Response<LzyResponse<Object>> response) {
////                                city.ispush = 1;
////                                notifyDataSetChanged();
//                            }
//                        });
//                        ApiService.followinfo_add(this, item.ispush == 1? 0:1, item.catid, item.chid, new DialogCallback<LzyResponse<Object>>(mContext) {
                        ApiService.followinfo_add(this, city.catid, city.chid, city.areaid, new DialogCallback<LzyResponse<Object>>(mContext) {
                            @Override
                            public void onSuccess(Response<LzyResponse<Object>> response) {
                                cityAdapter.notifyDataSetChanged();

                            }
                        });
                    }

                }
            });
        } else {
            helper.addOnClickListener(R.id.iv_type);
//            helper.setImageResource(R.id.iv_type, R.mipmap.tianjiaguanzhu);

            recLv.setVisibility(View.GONE);

        }

    }

    public void setSelect(Attention select) {
        select.isSelect = !select.isSelect;
        notifyDataSetChanged();
    }

    public void setIsExpend(Attention select) {
        select.isExpend = !select.isExpend;
        notifyDataSetChanged();
    }
}
